/**
 * @file interface.c
 * @brief
 *
 * @author Mr.Lin (Email：61900302@qq.com)
 * @version 1.0
 * @date 2023-09-04
 *
 * @copyright Copyright (c) 2023 KangTuo Tech.co, Ltd
 *
 * @par 修改日志:
 * Date           Version     Author      Description
 * 2023-09-04     1.0         Mr.Lin      Init
 */
#include "interface.h"
#include "lvgl.h"
#include "lv_port_disp_template.h"
#include "lv_port_indev_template.h"
#include "ui.h"
#include "cmsis_os.h"
#include "string.h"
#include "imath.h"
#include "stdio.h"
#include "user_bluetooth.h"
#include "dev_bluetooth.h"
#include "user_data.h"
#include "dev_bm1000.h"
#include "dev_battery.h"
#include "fault.h"
#include "user_fault.h"
#include "mlog.h"

LV_IMG_DECLARE(ui_img_25_percent_png);
LV_IMG_DECLARE(ui_img_50_percent_png);
LV_IMG_DECLARE(ui_img_75_percent_png);
LV_IMG_DECLARE(ui_img_100_percent_png);
LV_IMG_DECLARE(ui_img_charge_full_png);
LV_IMG_DECLARE(ui_img_lightning_png);

LV_IMG_DECLARE(ui_img_on_png);
LV_IMG_DECLARE(ui_img_off_png);

LV_IMG_DECLARE(ui_img_comfort_1l_png);
LV_IMG_DECLARE(ui_img_health_2l_png);
LV_IMG_DECLARE(ui_img_pilot_3l_png);

LV_IMG_DECLARE(ui_img_speaker_on_png);
LV_IMG_DECLARE(ui_img_speaker_off_png);

LV_IMG_DECLARE(ui_img_bluetooth_on_png);
LV_IMG_DECLARE(ui_img_bluetooth_off_png);

LV_IMG_DECLARE(ui_img_oximeter_on_png);
LV_IMG_DECLARE(ui_img_oximeter_off_png);

static void lv_send_a_inumber(lv_obj_t *obj, uint16_t number)
{
    char msg[20];
    sprintf(msg, "%d", number);
    lv_label_set_text(obj, msg);
}
/* 主机档位处理及显示 */
void lv_master_gear_handler(void)
{
}

/* 当前海拔高度大气环境含氧量 */
static uint16_t lv_environment_o2_concent_set(uint16_t altitude)
{
    return 0 ;
}

static void lv_send_temperature(void)
{
    lv_label_set_text(ui_LabelTemperature, "0℃");
}

int32_t a = 0;
void lv_send_number(void)
{
    static uint16_t index = 0;
    index++;
    switch(index) {
    //高度
    case 1:
        lv_label_set_text_fmt(ui_LabelAltitude, "%d", a++);
        break;
    //产品气氧气浓度
    case 2:
        lv_send_a_inumber(ui_LabelO2Concent, 95);
        break;
    //氧气含量
    case 3:
        lv_send_a_inumber(ui_LabelO2, lv_environment_o2_concent_set(20));
        break;
    //心率
    case 4:
        lv_send_a_inumber(ui_LabelHeartRate, 99);
        break;
    //血氧浓度
    case 5:
        lv_send_a_inumber(ui_LabelSpO2, 88);
        break;
    //环境压力
    case 6:
        lv_send_a_inumber(ui_LabelAmbientPressure, 96);
        break;
    //温度
    // case 7:lv_send_a_inumber(ui_LabelTemperature, rprotocol.msg.data.o2_temperature);break;
    case 7:
        lv_send_temperature();
        break;
    default:
        index = 0;
        break;
    }
}

/* 图表更新 */
void lv_chart_updata(uint32_t set_time)
{
    //    static uint32_t prev_time;
    //    uint32_t cur_time = get_secs();
    //    if((cur_time - prev_time)> set_time){
    //        lv_chart_set_next_value(ui_Chart2, ui_Chart2_series_1, (lv_coord_t)rprotocol.msg.data.altitude);
    //        prev_time = cur_time;
    //    }
}


/* 电量图标显示 */
void lv_battery_percent_set(lv_Screen_e screen, uint8_t bat)
{
    lv_obj_t *obj = NULL;
    if(screen == PAGE_HOME) {
        obj = ui_ImageBattery;
    }
    else if(screen == PAGE_SET) {
        obj = ui_ImageBattery2;
    }
    else if(screen == PAGE_OXIMETER) {    // 血氧界面
        obj = ui_ImageBattery3;
    }
    switch(bat) {
    case 0:
        lv_img_set_src(obj, &ui_img_25_percent_png);
        break;
    case 1:
        lv_img_set_src(obj, &ui_img_50_percent_png);
        break;
    case 2:
        lv_img_set_src(obj, &ui_img_75_percent_png);
        break;
    case 3:
        lv_img_set_src(obj, &ui_img_100_percent_png);
        break;
    default:
        break;
    }
}
/*
 * @brief 充电状态图标显示
 * @param screen : 页面
 */
void lv_battery_charge_set(lv_Screen_e screen)
{
    lv_obj_t *obj = NULL;
    if(screen == PAGE_HOME) {
        obj = ui_ImageBattery;
    }
    else if(screen == PAGE_SET) {
        obj = ui_ImageBattery2;
    }
    else if(screen == PAGE_OXIMETER) {
        obj = ui_ImageBattery3;
    }
    // lv_img_set_src(obj, &ui_img_charge_full_png);break;
    lv_img_set_src(obj, &ui_img_lightning_png);
}

/* 开关机图片按钮状态切换显示 */
void lv_power_state_set(lv_State_e state)
{
    switch(state) {
    case LV_ON:
        lv_imgbtn_set_src(ui_ImgButtonPowerOnOff, LV_IMGBTN_STATE_RELEASED, NULL, &ui_img_on_png, NULL);
        break;
    case LV_OFF:
        lv_imgbtn_set_src(ui_ImgButtonPowerOnOff, LV_IMGBTN_STATE_RELEASED, NULL, &ui_img_off_png, NULL);
        break;
    default:
        break;
    }
}

// uint8_t lv_power_state_get()
// {
//     lv_imgbtn_get_src_middle(ui_ImgButtonPowerOnOff, LV_IMGBTN_STATE_RELEASED);
// }

/* 主机图片按钮状态切换显示 */
void lv_master_gear_state_set(lv_MasterGearState_e state)
{
    switch(state) {
    case COMFORT_1L:
        lv_imgbtn_set_src(ui_ImgButtonMasterGear, LV_IMGBTN_STATE_RELEASED, NULL, &ui_img_comfort_1l_png, NULL);
        break;
    case HEALTH_2L:
        lv_imgbtn_set_src(ui_ImgButtonMasterGear, LV_IMGBTN_STATE_RELEASED, NULL, &ui_img_health_2l_png, NULL);
        break;
    case PILOT_3L:
        lv_imgbtn_set_src(ui_ImgButtonMasterGear, LV_IMGBTN_STATE_RELEASED, NULL, &ui_img_pilot_3l_png, NULL);
        break;
    default:
        break;
    }
}

/* 喇叭图片按钮状态显示 */
void lv_speaker_state_set(lv_State_e state)
{
    switch(state) {
    case LV_ON:
        lv_imgbtn_set_src(ui_ImgButtonSpeaker, LV_IMGBTN_STATE_RELEASED, NULL, &ui_img_speaker_on_png, NULL);
        break;
    case LV_OFF:
        lv_imgbtn_set_src(ui_ImgButtonSpeaker, LV_IMGBTN_STATE_RELEASED, NULL, &ui_img_speaker_off_png, NULL);
        break;
    default:
        break;
    }
}

/* 屏幕1蓝牙图片按钮状态显示 */
static void lv_bluetooth_state_set(lv_State_e state)
{
    switch(state) {
    case LV_ON:
        lv_imgbtn_set_src(ui_ImgButtonBluetoothOnOff, LV_IMGBTN_STATE_RELEASED, NULL, &ui_img_bluetooth_on_png, NULL);
        break;
    case LV_OFF:
        lv_imgbtn_set_src(ui_ImgButtonBluetoothOnOff, LV_IMGBTN_STATE_RELEASED, NULL, &ui_img_bluetooth_off_png, NULL);
        break;
    default:
        break;
    }
}

/* 屏幕2蓝牙图片状态显示 */
static void lv_bluetooth2_state_set(lv_State_e state)
{
    switch(state) {
    case LV_ON:
        lv_img_set_src(ui_ImageBluetoothSignal, &ui_img_bluetooth_on_png);
        break;
    case LV_OFF:
        lv_img_set_src(ui_ImageBluetoothSignal, &ui_img_bluetooth_off_png);
        break;
    default:
        break;
    }
}


/* 蓝牙link状态处理，及显示对应图标 */
void lv_bluetooth_set_link_status(uint8_t status)
{
    if(status == 1) {
        lv_bluetooth_state_set(LV_ON);
        lv_bluetooth2_state_set(LV_ON);
    }
    else {
        lv_bluetooth_state_set(LV_OFF);
        lv_bluetooth2_state_set(LV_OFF);
    }
}
/*
 * @brief 血氧仪蓝牙信号状态显示设置
 * @param state : 信号状态
 */
void lv_oximeter_signal_state_set(lv_State_e state)
{
    switch(state) {
    case LV_ON:
        lv_imgbtn_set_src(ui_ImgButtonOximeterSignal, LV_IMGBTN_STATE_RELEASED, NULL, &ui_img_oximeter_on_png, NULL);
        lv_img_set_src(ui_ImageOximeterSignal, &ui_img_oximeter_on_png);
        break;
    case LV_OFF:
        lv_imgbtn_set_src(ui_ImgButtonOximeterSignal, LV_IMGBTN_STATE_RELEASED, NULL, &ui_img_oximeter_off_png, NULL);
        lv_img_set_src(ui_ImageOximeterSignal, &ui_img_oximeter_off_png);
        break;
    default:
        break;
    }
}

/* 电池充电闪电图标显示 */
void lv_battery_lightning_set(uint8_t state)
{
    if(state == LV_OFF) {
        lv_obj_add_flag(ui_ImageLightning1, LV_OBJ_FLAG_HIDDEN);
        lv_obj_add_flag(ui_ImageLightning2, LV_OBJ_FLAG_HIDDEN);
        lv_obj_add_flag(ui_ImageLightning3, LV_OBJ_FLAG_HIDDEN);
    }
    else {
        lv_obj_clear_flag(ui_ImageLightning1, LV_OBJ_FLAG_HIDDEN);
        lv_obj_clear_flag(ui_ImageLightning2, LV_OBJ_FLAG_HIDDEN);
        lv_obj_clear_flag(ui_ImageLightning3, LV_OBJ_FLAG_HIDDEN);
    }
}


/* 蓝牙信号处理：根据信号显示不同的图片按钮状态 */
void lv_bluetooth_signal_handle(void)
{
    lv_bluetooth_state_set(LV_ON);
    lv_bluetooth2_state_set(LV_ON);
}



/*
 * @brief  开关机图标显示处理
 */
void lv_bluetooth_startup_switch_handle(void)
{
    lv_power_state_set(LV_ON);
}

/*
 * @brief  电池充电状态处理
 */
void lv_battery_charge_handle(void)
{
    lv_battery_charge_set(PAGE_HOME);
    lv_battery_charge_set(PAGE_SET);
    lv_battery_charge_set(PAGE_OXIMETER);
}

void lv_Click_prompt_handler(void)
{
    lv_obj_clear_flag(ui_ImageClickPrompt1, LV_OBJ_FLAG_HIDDEN);      // 显示图片}
}


